package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Brand;
import com.grom.po.BrandVisitHistory;
import com.grom.po.ContactInfo;
import com.grom.service.BrandHistoryService;
import com.grom.service.BrandService;
import com.grom.service.ContactInfoService;
import com.grom.util.IDCreator;
import com.grom.util.JsonUtil;
import com.grom.util.SecurityUtil;
import com.holder.log.ConsoleLog;

public class BrandAction {
	public void gotoList() {
		List<Brand> allBrands = BrandService.instance.getAllBrands();
		ContextHolder.setRequestAttribute("allBrands", allBrands);
		ContextHolder.forward("/WEB-INF/jsp/brand/update.jsp");
	}

	public void doSave() {
		Brand brand = (Brand) ContextHolder.initFromTheRequest(Brand.class);
		String contacts = ContextHolder.findInstance().getRequest().getParameter("contactInfo");
		String historyInfos = ContextHolder.findInstance().getRequest().getParameter("historyInfo");
		List<ContactInfo> conList = parseToContactInfo(contacts);
		List<BrandVisitHistory> historyList = parseToHistorys(historyInfos);
		ConsoleLog.debug(" | BrandAction | doSave | contacts | " + contacts);
		if (brand.getID() == null || brand.getID().trim().equals("")) {
			BrandService.instance.add(brand, conList, historyList);
		} else {
			BrandService.instance.update(brand, conList, historyList);
			;
		}
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri() + "/BrandAction/gotoList.do");
	}

	private List<ContactInfo> parseToContactInfo(String contacts) {
		List<ContactInfo> result = new ArrayList<ContactInfo>();
		if (contacts == null || contacts.indexOf("-") == 0) {
			return result;
		}
		if (contacts == null || contacts.trim().length() == 0) {
			return result;
		}
		String[] conList = contacts.trim().split(",");
		for (String str : conList) {
			String[] nameAndPhone = str.split("-");
			ContactInfo info = new ContactInfo();
			info.setContactPerson(nameAndPhone[0]);
			info.setContactPhone(nameAndPhone[1]);
			info.setID(IDCreator.getSingleId());
			result.add(info);
		}
		return result;
	}

	private List<BrandVisitHistory> parseToHistorys(String content) {
		List<BrandVisitHistory> result = new ArrayList<BrandVisitHistory>();
		if (content == null || content.trim().length() == 0) {
			return result;
		}
		String[] conList = content.trim().split("-");
		for (String str : conList) {
			BrandVisitHistory history = new BrandVisitHistory();
			history.setID(IDCreator.getSingleId());
			history.setDetailInfo(str);
			result.add(history);
		}
		return result;
	}

	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest().getParameter("itemid");
		Brand brand = BrandService.instance.getBrandById(id);
		String json = JsonUtil.parseToJson(brand);
		List<ContactInfo> infos = ContactInfoService.instance.getContactInfoByReId(brand.getID());
		String infoStr = JsonUtil.parseToJson(infos);
		List<BrandVisitHistory> historyList = BrandHistoryService.instance.getHistoryByBrandId(brand.getID());
		String historys = JsonUtil.parseToJson(historyList);
		json = json.replace("}", ",'contactInfo':" + infoStr + ", 'historys':" + historys + "}");
		ConsoleLog.debug("| BrandAction | getDetail | json | " + json);
		HttpServletResponse response = ContextHolder.findInstance().getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("brand=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest().getParameter("deleteId");
		System.out.println("| BrandAction | doDelete | id | " + id);
		Brand brand = BrandService.instance.getBrandById(id);
		if (!SecurityUtil.isUserHasRight(brand.getUserId())) {
			ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri() + "/BrandAction/gotoList.do");
			return;
		}
		brand.setID(id);
		BrandService.instance.delete(brand);
		ContactInfoService.instance.deleteByReId(id);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri() + "/BrandAction/gotoList.do");
	}

	public void conditionSelect() {
		String clothType_s = ContextHolder.findInstance().getRequest().getParameter("clothType_s");
		String brandName_s = ContextHolder.findInstance().getRequest().getParameter("brandName_s");
		String sql = "select * from brand where clothType like '%" + clothType_s + "%' and brandName like '%"
				+ brandName_s + "%'";
		List<Brand> allBrands = BrandService.instance.selectByCondition(sql);
		ContextHolder.setRequestAttribute("allBrands", allBrands);
		ContextHolder.forward("/WEB-INF/jsp/brand/update.jsp");
	}
}
